package com.cityeye.dao;

import java.util.List;

import com.cityeye.entity.Department;
import com.cityeye.entity.Loginuser;
import com.cityeye.search.SearchCriteria;

public class LoginUserDao extends AbstractEntityDao<Loginuser>{

	private static SearchCriteria[] criteria;

	public Loginuser createNewEmptyEntity() {
		return new Loginuser();
	}
	
	protected Object evalueateProperty(String key, Object valueObj) {
		String idStr = valueObj.toString();

		if (key.equalsIgnoreCase("department")) {
			return sf.getCurrentSession().get(Department.class,
					Integer.parseInt(idStr));
		}

		return valueObj;
	}	

	public SearchCriteria[] getSearchCriteria() {
		if (criteria == null) {
			criteria = new SearchCriteria[] {
					new SearchCriteria("name", SearchCriteria.VT_STRING),
					new SearchCriteria("password", SearchCriteria.VT_STRING)
					};
		}
		return criteria;
	}

	
	protected Class<Loginuser> getEntityClass(){
		return Loginuser.class;
	}
	
	public Loginuser login(String username, String password) {
		Loginuser loginuser = null;

		List<Loginuser> results = findByProperty(Loginuser.class, "name", username);

		if(results.size()>0)
		{
			loginuser = (Loginuser)results.get(0);
			if(password.equals(loginuser.getPassword())){
				return loginuser;
			}
		}
		return null;
	}

	public boolean isExist(String username) {
		Loginuser loginuser = null;

		List<Loginuser> results = findByProperty(Loginuser.class, "name", username);

		if(results.size()>0)
		{
			return true;
		}
		return false;
	}
}
